//
//  NavBar.m
//  微看
//
//  Created by 吴明磊 on 15/8/17.
//  Copyright © 2015年 wuminglei. All rights reserved.
//
#import "NavBar.h"
#import "UIButton+Extension.h"
#define Margin 5
#define FontSize 13


@interface NavBar ()

@property(nonatomic,strong)UIButton *myPerson;// 个人
@property(nonatomic,strong)UIButton *searchBtn;// 搜索
@property(nonatomic,strong)UIView *menuListView; // 菜单
@property(nonatomic,strong)UIView *barScrollLine; // 底部滚动条
@property(nonatomic,strong)NSLayoutConstraint *scrollLineCons; // 滚动条约束
@property(nonatomic,assign)BOOL isOpenMyPerson;
@end
@implementation NavBar

/// 加载自定义bar
- (void)loadBar {

    [self setupUI];
    

}

- (instancetype)init {

    self = [super init];
    
    [self notificationCenter];
    
    self.isOpenMyPerson = YES;
    
    return self;
}

- (void)scrollLineToIndex:(NSInteger)index {
    
   self.scrollLineCons.constant = ScreenSize.width * 0.2 * index;
    
    [UIView animateWithDuration:0.5 animations:^{
        
        [self layoutIfNeeded];
    }];
   
}

//MARK: 通知
/// 通知中心
- (void)notificationCenter {
    
    // 接收collectionView滚动
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:@"contentScrollNotification" object:nil];
}
/// navBar通知
- (void)receiveNotification:(NSNotification *)notification {
    
    NSInteger index = [[notification.userInfo objectForKey:@"index"] longValue];

    
    //TODO: 滚动条滚动
    [self scrollLineToIndex:index];
    
}

//MARK: 监听方法
/// 搜索点击
//TODO: 搜索点击
- (void)searchClick {
    
    NSLog(@"搜索");
}
///  个人点击
//TODO: 个人点击
- (void)myPersonClick {
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"myPersonClickNotification" object:nil userInfo:@{@"isOpen" : @(self.isOpenMyPerson)}];
    
    self.isOpenMyPerson = !self.isOpenMyPerson;
}

- (void)menuTabClick:(UIButton *)sender {
    
    [self scrollLineToIndex:sender.tag];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"NavBarModuleListSwitchNotification" object:nil userInfo:@{@"index" : @(sender.tag)}];
    
}

- (void)layoutSubviews {
    
    [self prepareLeftBtn];
    [self prepareSearchBtn];
    [self prepareMenuList];
    [self prepareMenuTab];
    [self prepareBarScrollLine];
    
    
    
}

//MARK: 准备UI
- (void)setupUI {
    
    [self addSubview:self.myPerson];
    [self addSubview:self.searchBtn];
    [self addSubview:self.menuListView];
    
    [self setupMenuTab];
    [self addSubview:self.barScrollLine];
    
    self.barScrollLine.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self addScrollLineNslayoutCons];

    
}

/// 准备left按钮
- (void)prepareLeftBtn {
    
    self.myPerson.translatesAutoresizingMaskIntoConstraints = NO;
    

    [self.myPerson makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.equalTo(self).offset(5);
        make.width.offset(60);
        make.height.offset(20);
    }];
    
    
    
}
/// 准备搜索按钮
- (void)prepareSearchBtn {
    
    
    
    self.searchBtn.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self.searchBtn makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self).offset(5);
        make.right.equalTo(self).offset(-5);
        make.width.height.offset(20);
    }];
    
    
}
/// 准备菜单
- (void)prepareMenuList {
    
    CGFloat top = CGRectGetMaxY(self.myPerson.frame);
    
    self.menuListView.frame = CGRectMake(0, top + Margin, ScreenSize.width, self.bounds.size.height - top - 2 * Margin);
    
}
/// 设置菜单选项卡
- (void)setupMenuTab {
    
    
    [self.menuListView addSubview:[UIButton buttonWithTitle:@"头条" andTitleColor:[UIColor whiteColor] andFontSize:FontSize]];
    [self.menuListView addSubview:[UIButton buttonWithTitle:@"欢乐" andTitleColor:[UIColor whiteColor] andFontSize:FontSize]];
    [self.menuListView addSubview:[UIButton buttonWithTitle:@"MEN" andTitleColor:[UIColor whiteColor] andFontSize:FontSize]];
    [self.menuListView addSubview:[UIButton buttonWithTitle:@"精选" andTitleColor:[UIColor whiteColor] andFontSize:FontSize]];
    [self.menuListView addSubview:[UIButton buttonWithTitle:@"聊吧" andTitleColor:[UIColor whiteColor] andFontSize:FontSize]];
}
/// 准备菜单选项卡
- (void)prepareMenuTab {
    
    CGFloat tabH = self.menuListView.bounds.size.height;
    CGFloat tabW = 30;
    NSInteger count = self.menuListView.subviews.count;
    
    CGFloat margin = ([UIScreen mainScreen].bounds.size.width - count * tabW ) / (count * 2);
    
    for (int i = 0; i < count; i ++) {
        
        UIButton *btn = self.menuListView.subviews[i];
        
        CGFloat tabX = margin + i * tabW + 2 * i * margin;
        
        btn.frame = CGRectMake(tabX, 0, tabW, tabH);
        
        btn.tag = i;
        
        [btn addTarget:self action:@selector(menuTabClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}
 /// 准备底部滚动条

- (void)prepareBarScrollLine {


   [self.barScrollLine makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self);
        make.width.offset([UIScreen mainScreen].bounds.size.width * 0.2);
        make.height.offset(4);
    }];

    
}

- (void)addScrollLineNslayoutCons {

    self.scrollLineCons = [NSLayoutConstraint constraintWithItem:self.barScrollLine attribute:NSLayoutAttributeLeft relatedBy:0 toItem:self attribute:NSLayoutAttributeLeft multiplier:1 constant:0];
    
    [self addConstraint:self.scrollLineCons];
}

//MARK: 懒加载

// 个人信息
- (UIButton *)myPerson {
    
    if (!_myPerson) {
        
        _myPerson = [UIButton buttonWithTitle:@" [匿名]" andTitleColor:[UIColor whiteColor] andFontSize:10 andImage:@"btn_sex_male" andBgImage:nil];
        [_myPerson addTarget:self action:@selector(myPersonClick) forControlEvents:UIControlEventTouchUpInside];
        [_myPerson sizeToFit];
    }
    return _myPerson;
}
// 搜索
- (UIButton *)searchBtn {
    
    if (!_searchBtn) {
        _searchBtn = [UIButton buttonWithImage:@"gr_search" andBgImage:nil];
        [_searchBtn addTarget:self action:@selector(searchClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchBtn;
}
// 菜单
- (UIView *)menuListView {
    
    if (!_menuListView) {
        _menuListView = [[UIView alloc]init];
    }
    return _menuListView;
}
// 底部滚动条
- (UIView *)barScrollLine {

    if (!_barScrollLine) {
        _barScrollLine = [[UIView alloc]init];
        _barScrollLine.backgroundColor = [UIColor whiteColor];
        _barScrollLine.alpha = 0.5;
    }
    return _barScrollLine;
}

@end
